home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0072_BASM Right Padded String.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  2KB  |  65 lines

  1. {
  2. From: GLENN CROUCH
  3. Subj: BASM STRING ROUTINES
  4. ---------------------------------------------------------------------------
  5. The following has been adjusted to do word aligned operations where possible
  6. for speed:
  7. }
  8.  
  9. {$A+}
  10. const
  11.      PadCh : CHAR = #32;
  12.  
  13. function PadRightStr (const S : string; Len : Byte) : string; assembler;
  14.  
  15.      { S is the String to Pad, Len is the length of the resultant String
  16.           Function adds Spaces to the Right of the String until Length is
  17.           Achieved. if Length (S) >= Len, then S is returned }
  18.  
  19. asm
  20.           mov dx, ds          { Save DS Register }
  21.           cld                 { Clear Direction Flag }
  22.           les di, @Result     { ES:DI => OutGoing String }
  23.           lds si, [S]         { DS:SI => Incoming String }
  24.           lodsb               { Read Length }
  25.           mov bh, al          { Store Length in BH }
  26.           sub ah, ah          { Set AH to 0 }
  27.           mov cx, ax          { Load CX with Current Length }
  28.           mov bl, [Len]       { Load Length of Dest into BL }
  29.           cmp al, bl
  30.           jnb @2
  31.           mov  al, bl           { Write Length }
  32.      @2:  stosb
  33.  
  34.       { Copy String }
  35.  
  36.           jcxz @1             { Ensure that there is some string to Copy }
  37.           movsb               { Move first char so stay word aligned }
  38.           dec  cx
  39.           jcxz @1
  40.           shr cx,1            { CX <- CX div 2 }
  41.           rep movsw           { move rest as words }
  42.           jnc @1              { if carry then odd number }
  43.           movsb               { so move the odd one }
  44.  
  45.      { Padding }
  46.  
  47.      @1:  sub   bl, bh          { Calculate how many spaces }
  48.           jna  @3             { if <= 0 then no padding needed }
  49.           sub cx, cx          { Load CX with No. of Spaces }
  50.           mov cl, bl
  51.           mov al, ' '         { place pad character into al }
  52.           shr bh, 1           { if original length was even then not word
  53.                              aligned }
  54.           jc @4
  55.           stosb               { Write first space to keep word aligned }
  56.           dec cx
  57.           jcxz @3
  58.      @4:  mov ah, al          { place ' ' also in Ah }
  59.           shr cx, 1           { Move Words }
  60.           rep stosw
  61.           jnc  @3             { Check if even number }
  62.           stosb               { Move odd space if any }
  63.      @3:  mov ds, dx          { Restore DS Register }
  64. end;
  65.